package com.example.dbsample.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dbsample.mapper.SysParamsMapper;
import com.example.dbsample.model.entity.SysParamsEntity;
import com.example.dbsample.service.business.SysParamsService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * 参数管理;(sys_params)表服务实现类
 * @author : http://www.chiner.pro
 * @date : 2024-3-7
 */
@Service
public class SysParamsServiceImpl extends ServiceImpl<SysParamsMapper, SysParamsEntity> implements SysParamsService {
    //@Override
    //public SysParams querySysParamsById(Long id) {
    //    return null;
    //}
    //
    //@Override
    //public Integer getOneSysParams(Long id) {
    //    return 0;
    //}
    //
    //@Override
    //public Page<SysParams> paginQuery(SysParams sysParams, PageRequest pageRequest) {
    //    return null;
    //}
    //
    //@Override
    //public SysParams insertSysParams(SysParams sysParams) {
    //    return null;
    //}
    //
    //@Override
    //public SysParams updateSysParams(SysParams sysParams) {
    //    return null;
    //}
    //
    //@Override
    //public boolean deleteSysParamsById(Long id) {
    //    return false;
    //}
    //@Autowired
    //private SysParamsMapper sysParamsMapper;
    
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public SysParamsEntity querySysParamsById(Long id){
        return this.baseMapper.queryById(id);
    }

    /**
      * TODO
      *   limit的前面有空格
      * 获取唯一的元素
      *
      * @param id 主键
      * @return 实例对象
      */
     @Override
    public Integer getOneSysParams(Long id){
         // TODO 两种方式都可以查询，但奇怪就是没有查出来结果，待定
         SysParamsEntity sysParams2 = getOne(new LambdaQueryWrapper<SysParamsEntity>()
                 .eq(SysParamsEntity::getId, id)
                 .orderByDesc(SysParamsEntity::getUpdatedTime)
                 .last(" limit 1")
         );
         SysParamsEntity sysParams = getOne(Wrappers.<SysParamsEntity>lambdaQuery());

        //if(sysParams == null) {
        //    return 1;
        //}

        return sysParams.getParamType();

    }

    /**
     * 分页查询
     *
     * @param sysParams 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    public Page<SysParamsEntity> paginQuery(SysParamsEntity sysParams, PageRequest pageRequest){
        long total = this.baseMapper.count(sysParams);
        return new PageImpl<>(this.baseMapper.queryAllByLimit(sysParams, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sysParams 实例对象
     * @return 实例对象
     */
    public SysParamsEntity insertSysParams(SysParamsEntity sysParams){
        this.baseMapper.insert(sysParams);
        return sysParams;
    }

    /**
     * 更新数据
     *
     * @param sysParams 实例对象
     * @return 实例对象
     */
    public SysParamsEntity updateSysParams(SysParamsEntity sysParams){
        this.baseMapper.update(sysParams);
        return this.querySysParamsById(sysParams.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteSysParamsById(Long id){
        int total = this.baseMapper.deleteById(id);
        return total > 0;
    }
}